package ngames.com.sushitv.action;

import android.app.Activity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.adapter.AnimeSingleVideoAdapter;
import ngames.com.sushitv.adapter.VideoRankAdapter;
import ngames.com.sushitv.base.BaseListAction;
import ngames.com.sushitv.core.ModelCallback;
import ngames.com.sushitv.entity.ItemVideo;
import ngames.com.sushitv.entity.UpUser;
import ngames.com.sushitv.entity.VideoBase;
import ngames.com.sushitv.entity.VideoDetails;
import ngames.com.sushitv.view.FlowLayout;

/**
 * Created by Jeffrey on 2016/7/27.
 */
public class FanMadeIntroAction extends BaseListAction {
    private TextView titleTxt, shareTxt, favTxt, rewardTxt, introTxt, upnameTxt, uploadsTxt, followersTxt;
    private RecyclerView recyclerView1, recyclerView2;
    private ImageView upImg;
    public FanMadeIntroAction(Activity activity, View baseView) {
        super(activity, baseView);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        recyclerView1 = (RecyclerView) mBaseView.findViewById(R.id.recyclerView1);
        recyclerView2 = (RecyclerView) mBaseView.findViewById(R.id.recyclerView2);
        final TextView textView = (TextView) mBaseView.findViewById(R.id.intro_text);
        titleTxt = (TextView) mBaseView.findViewById(R.id.title);
        shareTxt = (TextView) mBaseView.findViewById(R.id.text_share);
        favTxt = (TextView) mBaseView.findViewById(R.id.text_fav);
        rewardTxt = (TextView) mBaseView.findViewById(R.id.text_reward);
        introTxt = (TextView) mBaseView.findViewById(R.id.intro_text);
        upnameTxt = (TextView) mBaseView.findViewById(R.id.name_main);
        uploadsTxt = (TextView) mBaseView.findViewById(R.id.uploads_count);
        followersTxt = (TextView) mBaseView.findViewById(R.id.followers_count);
        upImg= (ImageView) mBaseView.findViewById(R.id.logo_main);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setMaxLines(Integer.MAX_VALUE);
            }
        });




        recyclerView1.setLayoutManager(new GridLayoutManager(mActivity, 2));
        recyclerView2.setLayoutManager(new LinearLayoutManager(mActivity));

        initData();
        onRefreshData();
    }

    private void initData() {

        getAppModel().videoDetails(AppApplication.getmVsId(), new ModelCallback<ArrayList<ItemVideo>>() {
            @Override
            public void onBeforeRequest(String urlTag) {
                Log.e("request", urlTag);
            }

            @Override
            public void onSuccess(ArrayList<ItemVideo> data) {
                setData();
            }

            @Override
            public void onFailure(String errorEvent, String message) {

            }
        });
    }

    private void setData() {
        VideoDetails v = AppApplication.getmVideoDetails();
        VideoBase vb = v.getBase();
        UpUser up = v.getUser();
        titleTxt.setText(vb.getTitle());
        shareTxt.setText(vb.getShare_count());
        favTxt.setText(vb.getFav_count());
        rewardTxt.setText(vb.getReward_count());
        introTxt.setText(vb.getVideo_summary());

        upnameTxt.setText(up.getName());
        uploadsTxt.setText("Uploads: "+up.getUploads());
        followersTxt.setText("Followers: "+up.getFollows());

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                    /*.showStubImage(R.drawable.image_is_loading)            // 设置图片下载期间显示的图片
                    .showImageForEmptyUri(R.drawable.image_loading_failed)    // 设置图片Uri为空或是错误的时候显示的图片
                    .showImageOnFail(R.drawable.image_loading_failed)        // 设置图片加载或解码过程中发生错误显示的图片
                   */
                .displayer(new RoundedBitmapDisplayer(190))
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)                            // 设置下载的图片是否缓存在SD卡中
                .build();
        String workerImgStr = up.getAvatar();
        ImageLoader.getInstance().displayImage(workerImgStr, upImg, options);
        VideoRankAdapter adapter = new VideoRankAdapter(mActivity, v.getSeries());
        //FanMadeIntroVideoAdapter adapter = new FanMadeIntroVideoAdapter(mActivity, fanMadeVideos);
        List<String> strings = new ArrayList<>();
        strings.add("Release Time");
        strings.add("Video");
        strings.add("Comments");
        strings.add("Danmaku");

        FlowLayout flowLayout = (FlowLayout) mBaseView.findViewById(R.id.flow_layout);
        flowLayout.setIfHide(false);
        flowLayout.setText(strings);
        AnimeSingleVideoAdapter adapter1 = new AnimeSingleVideoAdapter(mActivity, v.getRecommends());

        recyclerView1.setAdapter(adapter);
        recyclerView2.setAdapter(adapter1);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefreshData() {
    }

    @Override
    public void loadMore() {
//        videoRank();
    }


}
